﻿/*!	@file	r_gui_widget_textedit.cpp
	@brief	Window_GUI
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "r_gui_widget_textedit.h"
#include "r_gui_server.h"
#include "r_gui_draw.h"
#include "r_gui_event.h"
#include "../input/r_input_server.h"
#include "../input/r_input_device_keys.h"

// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
namespace r{
	namespace gui{
RRTTI_REFLECTION_IMPLEMENT ( r::gui::WidgetTextEdit, r::gui::Widget )
// <<<<<<<<<< RTTI <<<<<<<<<<


		WidgetTextEdit :: WidgetTextEdit()
			:
			hasFocus ( false ),
			cursorPos ( 0 ),
			selectFrom ( 0xffffffff )
		{
			minWidth	= 12;
			minHeight	= 12;

			posX		= 0;
			posY		= 0;
			posW		= 64;
			posH		= minHeight;
			posOffL		= posOffR		= 4;
			posOffT		= posOffB		= 1;
		}

		WidgetTextEdit :: ~WidgetTextEdit()
		{
		}

		void	WidgetTextEdit :: onUpdate ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onDraw ( draw::Draw& guidraw )
		{
			// 自分の領域にあわせて描画領域を作成
			guidraw.setZ ( guidraw.getZ() + 1 );
			r_si32	px;
			r_si32	py;
			r_si32	pw;
			r_si32	ph;
			r_si32	nx;
			r_si32	ny;
			r_si32	nw;
			r_si32	nh;
			guidraw.getRect ( px, py, pw, ph );
			if ( guidraw.calcRect ( px, py, pw, ph, wposX, wposY, posW, posH, nx, ny, nw, nh ) == true ) {
				guidraw.asignGUITexture();
				guidraw.setRect ( nx, ny, nw, nh );
				guidraw.asignRect();

				Server::instance		gui;
				const DrawInfo&		guiinfo = gui->getDrawInfo();

				if ( getVisible() == eRGUIVisible_Ghost ) {
					guidraw.drawWindow ( wposX, wposY, posW, posH, draw::WIndows_FlatLArc, guiinfo.passivePartColor );
					if ( editName.length() > 0 ) {
						// テキストの描画
						guidraw.drawText ( wposX + posOffL, wposY + posOffT, editName.c_str(), guiinfo.passiveCharColor );
					}
				} else {
					if ( getIsHover() == true ) {
						guidraw.drawWindow ( wposX, wposY, posW, posH, draw::WIndows_FlatLArc, guiinfo.hoverPartColor );
					} else {
						guidraw.drawWindow ( wposX, wposY, posW, posH, draw::WIndows_FlatLArc, guiinfo.activePartColor );
					}
					//カーソルオフセットの計算
					r::math::Vector2D	texoff;
					guidraw.calcTextPixPos ( editName.c_str(), cursorPos, texoff );
					// テキスト始端を決定
					r_si32	texX				= static_cast<r_si32> ( texoff.x );
					texX						= texX < posW ? 0 : texX - posW;
					// フォーカスされていればカーソル表示
					if ( hasFocus == true ) {
						r_si32	curPos				= texX < posW ? static_cast<r_si32> ( texoff.x ) + posOffL : posW - posOffR;
						draw::Rect	crect;
						crect.y						= wposY + posOffT;
						crect.height				= posH - ( posOffT + posOffB );
						crect.uvX					= 0;
						crect.uvY					= 0;
						crect.uvWidth				= guidraw.pix2uv ( 1 );
						crect.uvHeight				= guidraw.pix2uv ( 1 );
						// 選択範囲
						if ( ( selectFrom == 0xffffffff ) || ( selectFrom == cursorPos ) ) {
							// カーソル
							crect.x					= wposX + curPos;
							crect.width				= 2;
						} else {
							guidraw.calcTextPixPos ( editName.c_str(), selectFrom, texoff );
							r_si32	selX			= static_cast<r_si32> ( texoff.x );
							// 範囲
							if ( selectFrom < cursorPos ) {
								selX				= selX < texX ? texX : selX;
								crect.x				= wposX + selX;
								crect.width			= curPos - selX;
							} else {
								selX				= selX < posW ? selX : posW;
								crect.x				= wposX + curPos;
								crect.width			= selX - curPos;
							}
						}
						guidraw.drarwRect ( crect, guiinfo.activeCharColor );
					}

					// テキスト
					if ( editName.length() > 0 ) {
						// テキストの描画
						guidraw.drawText ( wposX + posOffL - texX, wposY + posOffT, editName.c_str(), guiinfo.activeCharColor );
					}
				}
			}
			// 親の領域を復帰させる
			guidraw.setZ ( guidraw.getZ() - 1 );
			guidraw.setRect ( px, py, pw, ph );
		}

		void	WidgetTextEdit :: onClickInRectLMB ( Event& guiEvent )
		{
			guiEvent.setLMBDrag ( this );
			guiEvent.setKeyboardFocus ( this );
			r_si32	x;
			r_si32	y;
			guiEvent.getCurPos ( x, y );
			r_si32	tx	=	x - wposX;
			r_si32	ty	=	y - wposY;

		}

		void	WidgetTextEdit :: onDoubleClickInRectLMB ( Event& guiEvent )
		{
			guiEvent.setKeyboardFocus ( this );
			selectAll();
		}

		void	WidgetTextEdit :: onReleaseInRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onReleaseOutRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onDragLMB ( Event& guiEvent )
		{
			r_si32	b_x;
			r_si32	b_y;
			r_si32	n_x;
			r_si32	n_y;
			guiEvent.getDragPos ( b_x, b_y );
			guiEvent.getCurPos ( n_x, n_y );
		}

		void	WidgetTextEdit :: onDragHoverInRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onDragReleaseInRectLMB ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onHoverInRect ( Event& guiEvent )
		{
		}

		void	WidgetTextEdit :: onGetKeyboardFocus ( Event& guiEvent )
		{
			hasFocus	= true;
		}

		void	WidgetTextEdit :: onLostKeyboardFocus ( Event& guiEvent )
		{
			comit();
		}

		void	WidgetTextEdit :: onUpdateKeyboardFocus ( Event& guiEvent )
		{
			r::input::Server::instance		input;
			const r::input::DeviceKeys*	keys	= input->getDeviceKeys ( 0 );
			r_bool						shift	= keys->getPressKey ( r::input::DeviceKeys_LShift ) | keys->getPressKey ( r::input::DeviceKeys_RShift );
			for ( r_ui32 i = 0; i <= r::input::DeviceKeys_Bqt; ++i ) {
				if ( keys->getPushKey ( i ) == true ) {
					switch ( i ) {
					case r::input::DeviceKeys_Escape:
					case r::input::DeviceKeys_Enter:
					case r::input::DeviceKeys_Return:
						comit();
						guiEvent.moveKeyboardFocus();
						break;
					case r::input::DeviceKeys_Del:
						addDelete();
						break;
					case r::input::DeviceKeys_BS:
						addBackSpace();
						break;
					default:
						if ( shift && ( i >= r::input::DeviceKeys_A ) && ( i <= r::input::DeviceKeys_Z ) ) {
							addChara ( r::input::DeviceKeysCharMap[i] - 32 );
						} else {
							addChara ( r::input::DeviceKeysCharMap[i] );
						}
						break;
					}
					return;
				}
			}
			if ( keys->getPushKey ( r::input::DeviceKeys_Arrow_Left ) == true ) {
				if ( ( shift == true ) && ( selectFrom == 0xffffffff ) ) {
					beginSelect();
				} else if ( ( shift != true ) && ( selectFrom != 0xffffffff ) ) {
					endSelect();
				}
				moveCursorLeft();
			} else if ( keys->getPushKey ( r::input::DeviceKeys_Arrow_Right ) == true ) {
				if ( ( shift == true ) && ( selectFrom == 0xffffffff ) ) {
					beginSelect();
				} else if ( ( shift != true ) && ( selectFrom != 0xffffffff ) ) {
					endSelect();
				}
				moveCursorRight();
			}
		}

		void	WidgetTextEdit :: moveCursorLeft()
		{
			if ( cursorPos > 0 ) {
				--cursorPos;
			}
		}
		void	WidgetTextEdit :: moveCursorRight()
		{
			if ( cursorPos < editName.length() ) {
				++cursorPos;
			}
		}
		void	WidgetTextEdit :: beginSelect()
		{
			selectFrom	= cursorPos;
		}
		void	WidgetTextEdit :: endSelect()
		{
			selectFrom	= 0xffffffff;
		}
		void	WidgetTextEdit :: selectAll()
		{
			if ( editName.empty() != true ) {
				selectFrom	= 0;
				cursorPos	= editName.length();
			} else {
				selectFrom	= 0xffffffff;
			}
		}
		void	WidgetTextEdit :: addChara ( r_char code )
		{
			// 範囲選択中ならばその範囲の文字をすべて消す
			if ( ( selectFrom != 0xffffffff ) && ( selectFrom != cursorPos ) ) {
				clearSelect();
			}
			editName.insert ( editName.begin() + cursorPos, code );
			moveCursorRight();

		}
		void	WidgetTextEdit :: addBackSpace()
		{
			// 範囲選択中ならばその範囲の文字をすべて消す
			if ( ( selectFrom != 0xffffffff ) && ( selectFrom != cursorPos ) ) {
				clearSelect();
				return;
			}
			// 今のカーソル位置の左の文字削除する
			if ( cursorPos == 0 ) {
				return;
			}
			--cursorPos;
			editName.erase ( editName.begin() + cursorPos );
		}
		void	WidgetTextEdit :: addDelete()
		{
			// 範囲選択中ならばその範囲の文字をすべて消す
			if ( ( selectFrom != 0xffffffff ) && ( selectFrom != cursorPos ) ) {
				clearSelect();
				return;
			}
			// 今のカーソル位置の右の文字を削除する
			if ( cursorPos >= editName.length() ) {
				return;
			}
			editName.erase ( editName.begin() + cursorPos );
		}
		void	WidgetTextEdit :: clearSelect()
		{
			if ( selectFrom == 0xffffffff ) {
				return;
			}
			if ( selectFrom != cursorPos ) {
				if ( selectFrom < cursorPos ) {
					editName.erase ( editName.begin() + selectFrom, editName.begin() + cursorPos );
					cursorPos	= selectFrom;
				} else {
					editName.erase ( editName.begin() + cursorPos, editName.begin() + selectFrom );
					cursorPos	= cursorPos;
				}
			}
			endSelect();
		}
		void	WidgetTextEdit :: comit()
		{
			setName ( editName.c_str() );
			endSelect();
			hasFocus	= false;
		}

		r_si32	WidgetTextEdit :: calcOffset()
		{
			return 0;
		}
	}
}
